package com.enniswu.weitter.widget.adapter;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.enniswu.weitter.R;
import com.enniswu.weitter.entity.Theme;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ThemeAdapter extends RecyclerView.Adapter<ThemeAdapter.ThemeViewHolder> {

    private List<Theme> themes;
    private String current;
    private OnItemClickListener clickListener;

    public ThemeAdapter(List<Theme> themes) {
        this.themes = themes;
    }

    public void setCurrent(String current) {
        this.current = current;
    }

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @NonNull
    @Override
    public ThemeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_theme, parent, false);
        final ThemeViewHolder holder = new ThemeViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener == null)
                    return;
                int position = holder.getAdapterPosition();
                clickListener.onItemClick(v, position);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ThemeViewHolder holder, int position) {
        Theme theme = themes.get(position);

        holder.tvName.setText(theme.getName());
        if (theme.getSkinName().equals(current))
            holder.tvName.setTextColor(holder.tvName.getResources().getColor(R.color.colorAccent));
        else
            holder.tvName.setTextColor(Color.BLACK);
    }

    @Override
    public int getItemCount() {
        return themes.size();
    }

    static class ThemeViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_theme)
        ImageView ivTheme;
        @BindView(R.id.tv_name)
        TextView tvName;

        public ThemeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
